//
//  YYT_TheOffcialViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_TheOffcialViewController.h"
#import "QL_TheOffcialView.h"
#import "QL_TheOffcialModel.h"
#import <MJRefresh.h>
#import "QL_MVPlayerViewController.h"
#import "QL_OfficalWebViewController.h"

@interface QL_TheOffcialViewController ()<TheOffcialDelegate>

@property (nonatomic, strong)QL_TheOffcialView *OffciaView;

#pragma mark  RecommendView
// 推荐页面数组
@property (nonatomic, retain) NSMutableArray *offcialArray;
// 网络请求 参数
@property (nonatomic, assign) NSInteger num;
// 网络请求参数
@property (nonatomic, retain) NSString *area;



@end

@implementation QL_TheOffcialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    self.OffciaView = [[QL_TheOffcialView alloc] initWithFrame:self.view.bounds];
    _OffciaView.backgroundColor = WHITE_COLOR;
    [self.view addSubview:_OffciaView];
    //协议第五步
    self.OffciaView.theOffcialDelegate = self;
    self.offcialArray = [NSMutableArray array];
    
    [self addheader]; // 刷新
    [self addfooter];
}
#pragma mark -- 上拉刷新
-(void)addfooter {
    self.OffciaView.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num += 20;
            [self getData:self.num];
            [self.OffciaView.myTableView.mj_footer endRefreshing];
        });
    }];
    
}
#pragma mark -- 下拉刷新
-(void)addheader {
    self.OffciaView.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.num = 0;
            [self.offcialArray removeAllObjects];
            [self getData:self.num];
            [self.OffciaView.myTableView.mj_header endRefreshing];
        });
    }];
    [self.OffciaView.myTableView.mj_header beginRefreshing];
}
#pragma mark -- 网络请求
-(void)getData:(NSInteger)dataNum {
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *txtFilePath = [documentPath stringByAppendingPathComponent:@"/huancun.xml"];
    
    [QL_AFNTools getWithUrl:@"http://mapi.yinyuetai.com/suggestions/front_page.json?D-A=0" parameter:@{@"rn":@"640*540", @"v":@"3", @"offset":[NSString stringWithFormat:@"%ld", dataNum], @"size":@"20"} httpHeader:@{@"App-Id":@"10201029",@"Device-Id":@"5fc617ae1220d0969b23eaab4c8f02b5",@"Device-V":@"QW5kcm9pZF80LjQuNF8xMDgwKjE5MjBfMTEwMDA5MDAw"} resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = result;
        BOOL result1 = [array writeToFile:txtFilePath atomically:YES];
        if (!result1) {
            NSLog(@"添加失败");
        }
        for (NSDictionary *dic in array) {
            QL_TheOffcialModel *model = [[QL_TheOffcialModel alloc] initWithDictionary:dic];
            [self.offcialArray addObject:model];
        }
        self.OffciaView.tableViewArray = self.offcialArray;
        
        [self.OffciaView.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.offcialArray removeAllObjects];
        NSArray *array = [NSArray arrayWithContentsOfFile:txtFilePath];
        for (NSDictionary *dic in array) {
            QL_TheOffcialModel *model = [[QL_TheOffcialModel alloc] initWithDictionary:dic];
            [self.offcialArray addObject:model];

        }
        
        [self.OffciaView.myTableView reloadData];
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)pushToDetailVCtableview:(UITableView *)tableview didSelectIndexPath:(NSIndexPath *)indexPath{
    if ([[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"PROGRAM"] || [[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"VIDEO"] || [[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"FANART"]) {
        QL_MVPlayerViewController *mv = [[QL_MVPlayerViewController alloc] init];
        mv.playurl = [NSString stringWithFormat:@"%@", [self.offcialArray[indexPath.row] urls]];
        mv.headtitle = [self.offcialArray[indexPath.row] title];
        mv.ids = [NSString stringWithFormat:@"%@", [self.offcialArray[indexPath.row] ids]];
        mv.pic = [self.offcialArray[indexPath.row] posterPic];
        [self.navigationController pushViewController:mv animated:NO];
    } else if ([[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"ACTIVITY"] || [[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"INVENTORY"]) {
        QL_OfficalWebViewController *WebView = [[QL_OfficalWebViewController alloc] init];
        WebView.url = [self.offcialArray[indexPath.row] urls];
        [self.navigationController pushViewController:WebView animated:NO];
    } else if ([[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"PLAYLIST"] || [[self.offcialArray[indexPath.row] typezzz] isEqualToString:@"WEEK_MAIN_STAR"]) {
        QL_MVPlayerViewController *mv = [[QL_MVPlayerViewController alloc] init];
        mv.playurl = [NSString stringWithFormat:@"%@", [self.offcialArray[indexPath.row] urls]];
        mv.headtitle = [self.offcialArray[indexPath.row] title];
        mv.ids = [NSString stringWithFormat:@"%@", [self.offcialArray[indexPath.row] ids]];
        mv.pic = [self.offcialArray[indexPath.row] posterPic];
        mv.typeup = 1;
        [self.navigationController pushViewController:mv animated:NO];
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
